package com.naiyumi.common;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
/**
 * 
 * @author 王洋
 * @company 中国联通统集成有限公司山分
 * @date 2010-1-30
 */

public class DBResultset implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	private List rs = null;

	private int currRow = -1;

	private int rowCount = 0;

	// private Map currMap = null;

	private int colCount = 0;

	@SuppressWarnings("unchecked")
	public DBResultset(List rs) {
		this.rs = rs;
		initRows();
		initCols();
	}

	public void initRows() {
		if (rs != null) {
			rowCount = rs.size();
		}
	}

	@SuppressWarnings("unchecked")
	public void initCols() {
		if (rs != null && rs.size() >= 1) {
			Map entry = (Map) rs.get(0);
			colCount = entry.size();
		}
	}

	public int getRows() {
		return rowCount;
	}

	public int getCols() {
		return colCount;
	}
	
	public void initCur() {
		currRow = -1;
	}

	public boolean moveNext() {
		if (currRow < rowCount - 1) {
			currRow++;
			return (true);
		} else if (currRow == rowCount - 1) {
			currRow++;
			return (false);
		} else {
			return (false);
		}
	}

	@SuppressWarnings("unchecked")
	private Map getCurrEntry() {
		Map currMap = (Map) rs.get(currRow);
		return currMap;
	}

	@SuppressWarnings("unchecked")
	public String fieldByIndex(int index) {
		if (currRow > -1 && currRow < rowCount && index > -1
				&& index < colCount) {
			Map currMap = getCurrEntry();
			Object obj = null;
			if (currMap == null) {
				return null;
			}
			int i = 0;
			for (Object value : currMap.keySet()) {
				if (index == i) {
					obj = currMap.get(value.toString());
					if (obj == null || obj.toString().equals("null")) {
						return "";
					}
					return obj.toString();
				}
				i++;
			}
		}
		return "";
	}

	// Modify by mmCAtE, 2008/01/09, START
	// public String fieldByName(int name) {
	@SuppressWarnings("unchecked")
	public String fieldByName(String name) {
		// END
		if (currRow > -1 && currRow < rowCount) {
			Map currMap = getCurrEntry();
			if (currMap.containsKey(name)) {
				if (currMap.get(name) == null
						|| currMap.get(name).toString().equals("null")) {
					return "";
				}

				return currMap.get(name).toString();
			}
		}
		return "";
	}
}
